package edu.utd.text.game.designer.parsers;

import java.util.StringTokenizer;

import edu.utd.text.game.controller.commands.GameCommand;
import edu.utd.text.game.controller.parsing.AbstractParser;
import edu.utd.text.game.designer.commands.AddGameItemCommand;
import edu.utd.text.game.designer.commands.ConnectRoomsCommand;
import edu.utd.text.game.designer.commands.CreateRoomCommand;
import edu.utd.text.game.designer.commands.DeleteGameItemCommand;
import edu.utd.text.game.designer.commands.DeleteRoomCommand;
import edu.utd.text.game.designer.commands.DisplayGameItemCommand;
import edu.utd.text.game.designer.commands.DisplayRoomCommand;
import edu.utd.text.game.designer.commands.EditRoomCommand;
import edu.utd.text.game.designer.commands.ExitRequestCommand;
import edu.utd.text.game.designer.commands.ListRoomsCommand;
import edu.utd.text.game.designer.commands.LoadCommand;
import edu.utd.text.game.designer.commands.SaveCommand;
import edu.utd.text.game.domain.exceptions.UserInputParseException;

public class DesignerParser extends AbstractParser {

	public static final DesignerParser INSTANCE = new DesignerParser();
	
	private DesignerParser() { }
	
	//TODO : IMPLEMENT ME!
	public GameCommand parseInput(String input) 
			throws UserInputParseException
	{
		/*
		 * What can this thing do?
		 * 
		 * create room +
		 * delete room +
		 * edit room +
		 * display room +
		 * connect room +
		 * add object +
		 * remove object +
		 * display object +
		 * save current map +
		 * load saved map
		 * list current game map contents +
		 * 
		 */
		

		if (!input.isEmpty()) {
			try { return parseInputForCreateRoom(input);		}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForRemoveRoom(input);		}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForEditRoom(input);			}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForDisplayRoom(input);		} 	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForListRooms(input);			}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForConnectRooms(input);		} 	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForAddGameItem(input);		}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForRemoveGameItem(input);	}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForDisplayGameItem(input);	} 	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForSave(input); 				}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForLoad(input);				}	catch (UserInputParseException uipx) { /* Go on */ }
			try { return parseInputForExitRequest(input);		} 	catch (UserInputParseException uipx) { /* Go on */ }
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForCreateRoom(String input) 
			throws UserInputParseException
	{
		if (input.toLowerCase().trim().startsWith("create room")) {
			return new CreateRoomCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForRemoveRoom(String input) 
			throws UserInputParseException
	{
		if (input.toLowerCase().trim().startsWith("remove room")
				|| input.toLowerCase().startsWith("delete room")) {
			return new DeleteRoomCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForEditRoom(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("edit room")) {
			return new EditRoomCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForDisplayRoom(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		StringTokenizer tokenizer = new StringTokenizer(input);
		String firstWord = tokenizer.nextToken().trim().toLowerCase();
		if (firstWord.equals("show")
			|| firstWord.equals("display")
				|| firstWord.equals("get"))
		{
			if (tokenizer.hasMoreTokens()) {
				if (tokenizer.nextToken().trim().toLowerCase().equals("room")) {
					if (tokenizer.hasMoreTokens()) {
						String roomName = "";
						while (tokenizer.hasMoreTokens()) {
							roomName += tokenizer.nextToken() + " ";		
						}
						return new DisplayRoomCommand(roomName.trim());
					}
					return new DisplayRoomCommand();
				}
			}
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForListRooms(String input) 
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("list rooms")
			|| input.startsWith("show rooms"))
		{
			return new ListRoomsCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForConnectRooms(String input) 
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("connect room")) {
			return new ConnectRoomsCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForAddGameItem(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("add item")
			|| input.startsWith("add game item"))
		{
			return new AddGameItemCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForRemoveGameItem(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("delete item")
			|| input.startsWith("remove item"))
		{
			return new DeleteGameItemCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForDisplayGameItem(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		StringTokenizer tokenizer = new StringTokenizer(input);
		String firstWord = tokenizer.nextToken().trim();
		if (firstWord.equals("show")
			|| firstWord.equals("display")
				|| firstWord.equals("get"))
		{
			if (tokenizer.hasMoreTokens()) {
				if (tokenizer.nextToken().trim().equals("item")) {
					if (tokenizer.hasMoreTokens()) {
						String itemName = "";
						while (tokenizer.hasMoreTokens()) {
							itemName += tokenizer.nextToken() + " ";
						}
						return new DisplayGameItemCommand(itemName.trim());
					}
				}
				return new DisplayGameItemCommand();
			}
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForSave(String input)
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.startsWith("save")) {
			return new SaveCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForLoad(String input) 
			throws UserInputParseException
	{
		input = input.toLowerCase().trim();
		if (input.equals("load")) {
			return new LoadCommand();
		}
		throw new UserInputParseException();
	}
	
	public GameCommand parseInputForExitRequest(String input) 
			throws UserInputParseException
	{
		if (input.equalsIgnoreCase("quit")) {
			return new ExitRequestCommand();
		}
		throw new UserInputParseException();
	}
}
